package com.cc.pokergame;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.cc.pokergame.databinding.ActivityGameBinding;
import com.cc.pokergame.model.CardModel;
import com.cc.pokergame.model.GameCenter;
import com.cc.pokergame.utils.AnimatorUtils;

import java.util.ArrayList;
import java.util.Arrays;

public class GameActivity extends AppCompatActivity {
    // 拿到Binding对象
    private ActivityGameBinding gameBinding;
    // 创建游戏中心
    private GameCenter gameCenter = new GameCenter();
    // 判断游戏是否是第一局
    private boolean isFirst = true;

    // 发牌起始位
    private float startPlayerCardX = 0f;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        gameBinding = ActivityGameBinding.inflate(getLayoutInflater());
        setContentView(gameBinding.getRoot());

        // 获取页面跳转获数据
        Intent intent = getIntent();
        // 获取数据
        int bottomPot = intent.getIntExtra("bottomPot", 100);
        Toast.makeText(this, bottomPot+"", Toast.LENGTH_SHORT).show();

        gameCenter.setTableMoney(bottomPot);

        // 游戏中心初始化
        gameCenter.init(
                new ArrayList<>(
                        Arrays.asList(gameBinding.player1Container.getRoot(),
                                gameBinding.player2Container.getRoot())
                ),
                new ArrayList<>(
                        Arrays.asList(new CardModel(gameBinding.player1card, gameBinding.card1winner),
                                new CardModel(gameBinding.player2card, gameBinding.card2winner))
                )
        );

        // 调用拍桌下注钱的变化
        gameCenter.setOnMoneyChangeListener(money -> {
            gameBinding.tvPot.setText(money + "");
        });

        // 监听游戏结束
        gameCenter.setOnGameOverListener(() -> {
            isFirst = false;
            AnimatorUtils.alphaOut(gameBinding.operateList);
            AnimatorUtils.alphaOut(gameBinding.betContainer);
            gameBinding.startBtn.setText("再来一局");
            AnimatorUtils.alphaIn(gameBinding.startBtn);

        });

        // 监听事件
        initEvent();
    }

    public void initEvent() {
        gameBinding.startBtn.setOnClickListener(v -> {
            // 如果不是第一局 就重置游戏
            if (!isFirst) {
                gameCenter.resetGame();

            } else {
                startPlayerCardX = gameBinding.player1card.getX();
                // 显示筹码
                AnimatorUtils.alphaIn(gameBinding.chip);

                // 显示扑克
                AnimatorUtils.alphaIn(gameBinding.player1card);
                AnimatorUtils.alphaIn(gameBinding.player2card);

                // 显示玩家
                AnimatorUtils.alphaIn(gameBinding.player1Container.getRoot());
                AnimatorUtils.alphaIn(gameBinding.player2Container.getRoot());
            }

            // 点击按钮淡出
            AnimatorUtils.alphaOut(gameBinding.startBtn);

            // 下注总金额
            AnimatorUtils.alphaIn(gameBinding.betContainer);

            // 平移动画
            AnimatorUtils.translate(gameBinding.player1card, startPlayerCardX, gameBinding.player1placeHolder.getX());
            AnimatorUtils.translate(gameBinding.player2card, startPlayerCardX, gameBinding.player2placeHolder.getX());

            AnimatorUtils.alphaIn(gameBinding.operateList);

            // 点击开始游戏
            gameCenter.start();
        });

        gameBinding.ivGiveUp.setOnClickListener(v -> {
            gameCenter.giveUp();
        });
        gameBinding.ivAdd.setOnClickListener(v -> {
            gameCenter.betting();
        });
        gameBinding.ivPK.setOnClickListener(v -> {
            gameCenter.pk();
        });
    }
}